#!/usr/bin/env bash

include "libs/test.sh"

function cctl()
{
    #local color=$1
    #local other=$2
    case $1 in
        r) printf "\033[$231m";; # frontcolor red
        g) printf "\033[$232m";; # frontcolor green
        y) printf "\033[$233m";; # frontcolor yellow
        b) printf "\033[$234m";; # frontcolor blue
        p) printf "\033[$235m";; # frontcolor pink
        c) printf "\033[$236m";; # frontcolor cyan
        d) printf "\033[$237m";; # frontcolor drak white = grey
        w) printf "\033[$238m";; # frontcolor white
        0) printf "\033[$20m";;  # frontcolor reset
        bgr) printf "\033[$231m";; # background_color red
        bgg) printf "\033[$232m";; # background_color green
        bgy) printf "\033[$233m";; # background_color yellow
        bgb) printf "\033[$234m";; # background_color blue
        bgp) printf "\033[$235m";; # background_color pink
        bgc) printf "\033[$236m";; # background_color cyan
        bgd) printf "\033[$237m";; # background_color drak white = grey
        bgw) printf "\033[$238m";; # background_color white
        bg0) printf "\033[$20m";;  # background_color reset
        fg*)
            local r=$(printf "%d" "0x${1:2:2}")
            local g=$(printf "%d" "0x${1:4:2}")
            local b=$(printf "%d" "0x${1:6:2}")
            printf "\033[$238;2;$r;$g;${b}m"
            ;;
        bg*) 
            local r=$(printf "%d" "0x${1:2:2}")
            local g=$(printf "%d" "0x${1:4:2}")
            local b=$(printf "%d" "0x${1:6:2}")
            printf "\033[$248;2;$r;$g;${b}m"
            ;;
        *)
            if [ ${#1} = 6 ]; then
                local r=$(printf "%d" "0x${1:0:2}")
                local g=$(printf "%d" "0x${1:2:2}")
                local b=$(printf "%d" "0x${1:4:2}")
                printf "\033[$238;2;$r;$g;${b}m"
            fi
            ;;
    esac
}

function print:pathfile:get_pathfile()
{
    local filename=$1
    filename=$(sed 's@\.\./[^/]*/@@g' <<< $filename)
    filename=$(sed 's@\./@@g' <<< $filename)
    filename=$(dirname $filename)/$(basename $filename)

    while (( ${#*} >= 2 )); do
        local alias=$2
        local filename_with_alias=$(sed "s|${alias#*=}|${alias%%=*}|g" <<< $filename)
        if [ "$filename" != "$filename_with_alias" ]; then
            echo $filename_with_alias && return
        fi
        shift
    done
    echo $filename
}

function print:pathfile:get_pathfile:test()
{
    cd /home/

    test:run_cmd print:pathfile:get_pathfile '/home/karl/a.sh'
    test:should_be '/home/karl/a.sh'

    test:run_cmd print:pathfile:get_pathfile '/home/karl/a.sh' '$(pwd)='"$(pwd)"
    test:should_be '$(pwd)/karl/a.sh'

    test:run_cmd print:pathfile:get_pathfile '/home/karl/a.sh' '$(pwd)='"$(pwd)" 'karl=/home/karl'
    test:should_be '$(pwd)/karl/a.sh'

    test:run_cmd print:pathfile:get_pathfile '/home/karl/a.sh' '$karl=/home/karl' '$(pwd)='"$(pwd)"
    test:should_be '$karl/a.sh'
}
test:register_test print:pathfile:get_pathfile:test

function print:pathfile()
{
    local pathfile=$1 ; shift
    print:pathfile:get_pathfile $pathfile "\$(pwd)=$(pwd)" $* \
        | sed 's@\([^/]*/\?\)\(.*/\)\?\(.*\)@'$(cctl d)'\1 '$(cctl y)'\2'$(cctl r)'\3'$(cctl 0)'@g'
}

function print:pathfile:test()
{
    cd /home/

    test:run_cmd print:pathfile '/home/karl/a.sh'
    test:should_be "$(cctl d)\$(pwd)/ $(cctl y)karl/$(cctl r)a.sh$(cctl 0)"
}
test:register_test print:pathfile:test

circle_slider_index=0
circle_slider_str="⠇⡆⣄⣠⢰⠸⠙⠋"
function print:circle_slider()
{
    printf "\033[2K$(cctl p)${circle_slider_str:$circle_slider_index:1}$(cctl y)$1$(cctl 0)\r" 1>&2
    ((circle_slider_index=(circle_slider_index+1)%${#circle_slider_str}))
}

function report()
{
    local ret="$?"
    local message="$*"

    if [ $ret = 0 ]; then
        echo -e "\n$(cctl g)^_^ $COMMAND 成功: $message$(cctl 0)"
    else
        echo -e "\n$(cctl r)QaQ $COMMAND 失败: $message$(cctl 0)"
    fi

    return $ret
}

function output:logd()
{
    local fmt="$1"
    shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl c)$fmt$(cctl 0)\n" $*
}

function output:logi()
{
    local fmt="$1"
    shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl g)$fmt$(cctl 0)\n" $*
}

function output:logw()
{
    local fmt="$1"
    shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl y)$fmt$(cctl 0)\n" $*
}

function output:loge()
{
    local fmt="$1"
    shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl r)$fmt$(cctl 0)\n" $*
}
